package com.xinqing.test.algorithm.k8s.forecast;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.xinqing.test.modle.algorithm.ForecastParam;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:CHQ
 * @Date:2020/9/29 17:03
 * @Description
 */
public class ST5f46f0d0c8c4d6ff {
    public static void main(String args[]) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        ForecastParam forecastParam = new ForecastParam();
        forecastParam.setBusId("ad_wdl");

        Map<String, Object> featuresMap = new HashMap<>();
        featuresMap.put("c1","ios");
        featuresMap.put("c2","MyMoney");
        featuresMap.put("c5","splash");
        featuresMap.put("c6","appstore");
        featuresMap.put("t1","2021-04-12 11:15:10");
        featuresMap.put("u5","0");
        featuresMap.put("u7","0");
        featuresMap.put("u8","0");
        featuresMap.put("u11","0");
        featuresMap.put("u17","0");
        featuresMap.put("u18","0");
        featuresMap.put("u19","0");
        featuresMap.put("u23","0");
        featuresMap.put("u26","0");
        featuresMap.put("u28","0");
        featuresMap.put("u29","0");

        featuresMap.put("u31","0");
        featuresMap.put("u32","0");
        featuresMap.put("u33","0");
        featuresMap.put("u34","0");
        featuresMap.put("u36","0");
        featuresMap.put("u37","0");
        featuresMap.put("u38","0");
        featuresMap.put("u39","0");
        featuresMap.put("u40","0");
        featuresMap.put("u41","0");
        featuresMap.put("u42","0");
        featuresMap.put("u43","4");
        featuresMap.put("u44","5");
        featuresMap.put("u45","0");
        featuresMap.put("us0","0");
        featuresMap.put("us1","0");
        featuresMap.put("us2","0");
        featuresMap.put("us3","0");
        featuresMap.put("us4","0");
        featuresMap.put("n1","0");
        featuresMap.put("n2","0");
        featuresMap.put("n3","11");

        featuresMap.put("n4","0");
        featuresMap.put("n5","0");
        featuresMap.put("n6","0");
        featuresMap.put("n7","0");
        featuresMap.put("n8","11");
        featuresMap.put("n9","11");
        featuresMap.put("n10","0");
        featuresMap.put("y1","0");
        featuresMap.put("y2","0");
        featuresMap.put("y3","0");
        featuresMap.put("y4","0");
        featuresMap.put("y5","0");
        featuresMap.put("y6","0");
        featuresMap.put("y7","0");
        featuresMap.put("y8","0");
        featuresMap.put("y9","0");
        featuresMap.put("y10","0");
        featuresMap.put("y11","0");
        featuresMap.put("y12","0");
        featuresMap.put("y13","0");

        forecastParam.setFeatures(featuresMap);
       // "\"multiFeatures\":{\"97516\":{\"u32\":\"1\",\"us2\":\""0"147\",\"us4\":\"57\",\"u37\":\"0\",\"u36\":\"0\",\"u39\":\"1\",
        // \"u17\":\"0\",\"u38\":\"249\"}," +

        //多分类特征
        HashMap<String, Map<String, Object>> multiFeaturesMap = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("c3","57");
        map.put("c4","0");
        map.put("c7","SP");
        map.put("c0","17375");
        multiFeaturesMap.put("88888",map);
       // forecastParam.setMultiFeatures(multiFeaturesMap);
        forecastParam.setTraceId("45154178");

        //"\"models\":[\"693ba772fab3a1d8\",\"951c327c56779183\"]," +
    //    forecastParam.setModels(new String[]{"aa1435d0024cacf1","1d17ee93a0132f02"});
       // forecastParam.setModels(new String[]{"395fd9ded4eea134","0d00d361764b1831"});
      //  forecastParam.setModels(new String[]{"395fd9ded4eea134","eccb4f6022b885b0"});
        forecastParam.setModels(new String[]{"cda599e40b9a1ffa","428cc73eab481abf"});
        forecastParam.setTop(0);

        String data = JSON.toJSONString(forecastParam);

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

        String url = "https://sml.suishoudata.com/model/5f46f0d0c8c4d6ff";

        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            System.out.println("rslt:" + response.getResponseBody());
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }


    }
}
